package me.zhengjie.modules.system.rest;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.ResourceComplete;
import me.zhengjie.modules.system.domain.TeskAndstudent;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.ResourceCompleteService;
import me.zhengjie.modules.system.service.TeskAndstudentService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@Api(tags = "资源完成")
@RequestMapping("/api/resourceComplete")
public class ResourceCompleteController {

    private final ResourceCompleteService resourceCompleteService;

    private final TeskAndstudentService teskAndstudentService;

    private final UserService userService;
    @PostMapping
    @Log("资源完成")
    @ApiOperation("资源完成")
    public ResponseEntity<Object> resourceComplete(@RequestBody ResourceComplete resourceComplete) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("resourceId", resourceComplete.getResourceId());
        queryWrapper.eq("teskId", resourceComplete.getTeskId());
        ResourceComplete currentresourceComplete = resourceCompleteService.getOne(queryWrapper);
        User user = userService.findByName(SecurityUtils.getCurrentUsername());
        TeskAndstudent teskAndstudent = new TeskAndstudent();

        if(currentresourceComplete!=null){

            resourceComplete.setId(currentresourceComplete.getId());
            resourceComplete.setUserId(Math.toIntExact(user.getId()));

            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("teskId", resourceComplete.getTeskId());
            queryWrapper1.eq("userId", Math.toIntExact(user.getId()));
            teskAndstudentService.getOne(queryWrapper1);



            resourceCompleteService.updateById(resourceComplete);

            QueryWrapper queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("userId",user.getId());
            queryWrapper2.eq("teskId",resourceComplete.getTeskId());
            TeskAndstudent  teskAndstudent1 = teskAndstudentService.getOne(queryWrapper2);
            teskAndstudent1.setState(resourceComplete.getState());
            teskAndstudentService.updateTeskAndstudent(teskAndstudent1);;
        } else {
            resourceComplete.setUserId(Math.toIntExact(user.getId()));
            resourceCompleteService.save(resourceComplete);


            QueryWrapper queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("userId",user.getId());
            queryWrapper2.eq("teskId",resourceComplete.getTeskId());
            TeskAndstudent  teskAndstudent1 = teskAndstudentService.getOne(queryWrapper2);
            teskAndstudent1.setState(resourceComplete.getState());
            teskAndstudentService.updateTeskAndstudent(teskAndstudent1);;
        }
        return ResponseEntity.ok("资源完成");
    }
}
